package com.joplus.housekeeper.core;

import android.app.Activity;

import com.joplus.housekeeper.network.RequestHelper;
import com.joplus.housekeeper.network.TextCallback;
import com.joplus.housekeeper.utils.L;
import com.joplus.housekeeper.utils.StringUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * author wutingxi
 * time 2016/7/27 17:41
 * 已取消和已失效可以删除
 */
public enum OrderStatus {
    //已取消已失效才可以删除
    WAIT_PAY("100001", "待支付"),
    //赠品未发送
    HAD_PAY("100002", "已支付"),
    FINISH("100003", "已完成"),
    //包包未找到
    CANCEL("100004", "已取消"),
    DISABLE("100005", "已失效"),
    REFUNDED("100006", "已退款"),
    //定金已经支付
    DEPOSITE("100007", "定金已支付"),

    REFUSE("100009", "拒绝退款"),
    REFUNDING("100010", "退款中"),


    //包包需求订单
    BAG_FIND("100012", "受理中"),
    //赠品已发货
    GIFT_SEND("100013", "已发货");


    private String typecode;
    private String name;


    OrderStatus(String typecode, String name) {
        this.typecode = typecode;
        this.name = name;
    }


    public String getTypecode() {
        return typecode;
    }

    public String getName() {
        return name;
    }

    /**
     * 获取跳转页面的种类
     *
     * @param typecode
     * @return
     */
    public static OrderStatus getOrderType(String typecode) {
        if (StringUtils.isEmpty(typecode)) {
            //throw  new IllegalArgumentException("page type can't be null");
            return DISABLE;
        }
        for (OrderStatus pt : OrderStatus.values()) {
            if (pt.typecode.equals(typecode)) {
                L.d("ordertype", pt.toString());
                return pt;
            }
        }
        return DISABLE;
    }

    public static void checkOrderStatus(final Activity context, String orderId) {
        if (orderId == null) {
            context.finish();
        }
        RequestHelper.getInstance().orderStatus(context, orderId,
                new TextCallback() {
                    @Override
                    public void onSuccess(Object info) {

                        try {
                            JSONObject jsonObject = null;
                            final String orderInfo = (String) info;
                            jsonObject = new JSONObject(orderInfo);
                            final String orderStatus = jsonObject.getString("orderStatus");
                            switch (OrderStatus.getOrderType(orderStatus)) {
                                case WAIT_PAY:
                                    break;
                                case HAD_PAY:
                                case FINISH:
                                case CANCEL:
                                case DISABLE:
                                case REFUNDING:
                                case REFUNDED:
                                case REFUSE:
                                    context.finish();
                                    break;

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }

                    @Override
                    public void onFailure(String error) {


                    }
                });

    }

}
